#include "project.h"
#include <QFile>
#include <QTextStream>

projectC::projectC(QWidget *parent)
    : QDialog(parent)
{


    // creating layouts
    layall=new QVBoxLayout(this);
    linker=new QHBoxLayout(this);
    ccompiler=new QHBoxLayout(this);
    cppcompiler=new QHBoxLayout(this);

    // structuring layouts
    this->setLayout(layall);
    layall->addLayout(linker);
    layall->addLayout(ccompiler);
    layall->addLayout(cppcompiler);

    // linker fields
    linkerl=new QLabel("Linker:");
    linkerf=new QLineEdit();
    linker->addWidget(linkerl);
    linker->addWidget(linkerf);

    // c-compiler fields

    ccompilerl=new QLabel("C-Compiler:");
    ccompilerf=new QLineEdit();
    ccompiler->addWidget(ccompilerl);
    ccompiler->addWidget(ccompilerf);


    // c++ - compiler fields

    cppcompilerl=new QLabel("C++ Compiler:");
    cppcompilerf=new QLineEdit();
    cppcompiler->addWidget(cppcompilerl);
    cppcompiler->addWidget(cppcompilerf);


    save_button=new QPushButton("Save");
    layall->addWidget(save_button);
    this->setWindowTitle("Project options");
    this->resize(640,150);
    connect(save_button,SIGNAL(clicked()),this,SLOT(save()));

}

void projectC::init(){

       QString gs;


       if (!curpath.isEmpty()){

    QFile file(curpath+"/project.t3p" );
    if (file.open(QFile::ReadOnly | QFile::Text)) {
            gs=file.readAll();
            file.close();
            QStringList projopt=gs.split("\n",QString::KeepEmptyParts);

    Olinker=projopt.at(0);
    Occompiler=projopt.at(1);
    Ocppcompiler=projopt.at(2);


    linkerf->setText(Olinker);
    ccompilerf->setText(Occompiler);
    cppcompilerf->setText(Ocppcompiler);
save_button->setEnabled(true);
}
}else{

save_button->setEnabled(false);

}

}

void projectC::save(){

    Olinker=linkerf->text();
    Occompiler=ccompilerf->text();
    Ocppcompiler=cppcompilerf->text();
    this->close();

    if (!curpath.isEmpty()){
     QFile file(curpath+"/project.t3p");
     if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
         return;

     QTextStream out(&file);
     out <<Olinker<<"\n"<<Occompiler<<"\n"<<Ocppcompiler<<"\n";
 file.close();
 }
}
